package com.productline;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.apple.util.Configure;
import org.apple.util.PLEConfigure;
import org.jdom.Element;

public class PicturePanel extends JPanel implements Runnable {

	public Image image = null;
	public ImageIcon background = null;
	public Map<String, Boolean> sensorVisibility = new HashMap<String, Boolean>();
	public Map<String, Rectangle> sensorArea = new HashMap<String, Rectangle>();
	public Map<String, Image> sensorArea2 = new HashMap<String, Image>();

	public Thread animator;
	public int pos = 10;
	public boolean isTop = false;

	public PicturePanel() {
		this.setLayout(null);
		try {
			image = ImageIO.read(new File(".\\Generator\\dragon.png"));
			background = new ImageIcon(".\\Generator\\b.gif");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// JPanel back = new BackgroundPicture(background);
		// this.add(back);
		// back.setBounds(0, 0, 400, 500);

		// TODO: should do changes here
		// sensorVisibility.put("Wind Speed Sensor", false);

		// sensorArea.put("Wind Speed Sensor", new Rectangle(155, 20, 60, 70));

		animator = new Thread(this);
		animator.start();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(background.getImage(), 0, 0, this);
		g2.drawImage(image, 10, 10 + pos, this);
		g2.setStroke(new BasicStroke(8));
		for (String s : sensorVisibility.keySet()) {
			if (sensorVisibility.get(s)) {
				Rectangle rect = sensorArea.get(s);
				Image img = sensorArea2.get(s);
				g2.drawRect(rect.x, rect.y + pos, rect.width, rect.height);
				g2.drawImage(img, rect.x, rect.y + pos, rect.width,
						rect.height, null);

			}
		}

		g2.setStroke(new BasicStroke(2));

		if (isTop) {
			g2.drawOval(260, 100, 10, 10);
			g2.drawOval(280, 80, 20, 20);
			g2.drawOval(310, 50, 30, 30);
			g2.drawOval(350, 10, 40, 40);
		}
	}

	public void run() {

		int temp = 2;
		while (Thread.currentThread() == animator) {
			repaint();
			try {
				if (isTop) {
					Thread.sleep(1000);
					isTop = false;
				}
				Thread.sleep(100);
			} catch (InterruptedException e) {
				break;
			}
			pos += temp;
			if (pos < 0)
				isTop = true;
			if (pos > 90 || pos < 0)
				temp *= -1;
		}
	}

	public void setVisibility(String name, boolean v) {
		if (sensorVisibility.containsKey(name))
			this.sensorVisibility.put(name, v);
	}
}

class SensorActionListener implements ActionListener {
	public PicturePanel picturePanle;
	public SensorConfigurePanel configurationPanel;
	public String sensorName;
	private String sensorSelected;

	public SensorActionListener(String name, int x, int y,
			String sensorSelected, PicturePanel p, SensorConfigurePanel cp) {
		this.picturePanle = p;
		this.configurationPanel = cp;
		this.sensorName = name;
		this.sensorSelected = sensorSelected;
		this.picturePanle.sensorVisibility.put(name, false);
		this.picturePanle.sensorArea.put(name, new Rectangle(x, y, 60, 70));
		
			Image img = null;
			try {
				if (x != 0 && y != 0) {
				img = ImageIO.read(new File(".\\Generator\\" + name + ".png"));
				}
				else{
					img = ImageIO.read(new File(".\\Generator\\blank.gif"));
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.picturePanle.sensorArea2.put(name, img);
		
	}

	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if (obj instanceof JCheckBox) {
			JCheckBox jcb = (JCheckBox) obj;
			if (jcb.isSelected()) {
				picturePanle.setVisibility(sensorName, true);
				for (String s : configurationPanel.map.keySet())
					configurationPanel.map.get(s).setVisible(false);
				configurationPanel.map.get(sensorName).setVisible(true);
				PLEConfigure conf = PLEConfigure.instance();

				conf.setProperty(sensorSelected, "true");
				// System.out.println(sensorSelected);
			} else {
				picturePanle.setVisibility(sensorName, false);
				for (String s : configurationPanel.map.keySet()) {

					configurationPanel.map.get(s).setVisible(false);
				}
				configurationPanel.map.get(sensorName).setVisible(false);
				PLEConfigure conf = PLEConfigure.instance();

				conf.setProperty(sensorSelected, "false");
				// System.out.println(sensorSelected);
			}
		} else {
			JRadioButton jrb = (JRadioButton) obj;
			// picturePanle.setVisibility(sensorName, true);
			PLEConfigure conf = PLEConfigure.instance();

			conf.setProperty(sensorSelected, "true");

			List<Element> list = GeneratorFrame.doc.getRootElement().getChild(
					"position-sensor").getChildren("positionSensorType");
			for (Element el : list) {
				if (el.getChildText("name").equals(sensorName)) {
					conf.setProperty("NavigationDriver", el
							.getChildText("driver"));
					break;
				}

			}
			// System.out.println(sensorSelected);
		}

	}

}
//
// class BackgroundPicture extends JPanel{
//	
// Image image;
//	
// public BackgroundPicture(Image image){
// this.image = image;
// }
//	
// public void paintComponent(Graphics g){
// g.drawImage(image, 0, 0, this);
// }
// }
